package com.zyj.eps.controller;

import com.alibaba.fastjson.JSON;
import com.zyj.eps.pojo.BackHomeApply;
import com.zyj.eps.pojo.Inform;
import com.zyj.eps.pojo.User;
import com.zyj.eps.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * @author super
 * @date 2022/3/24 13:56
 */
@Controller
@RestController
@CrossOrigin
@SuppressWarnings("all")//抑制警告
public class UserController {
    @Autowired
    private UserServiceImpl userService;

    @RequestMapping("/getAllUser")
    public String getAllUser(){
        System.out.println("访问成功");
        HashMap<String, Object> data = new HashMap<>();
        List<User> list = userService.getAllUser();
        System.out.println(list);
        data.put("UserList", list);
        String UserList = JSON.toJSONString(data);
        return UserList;
    }
    @RequestMapping("/getUserByUUID")
    public String getUserByUUID(String UUID){
        User userByUUID = userService.getUserByUUID(UUID);
        if(userByUUID.getAge()==0){
            userByUUID.setAge(18);
        }
        return JSON.toJSONString(userByUUID);
    }
    @RequestMapping("/editUserInfo")
    public String editUserInfo(@RequestBody User user){
        int i = userService.editUserInfo(user);
        String flag = "error";
        if(i==1){
            flag="ok";
        }
        User userByUUID = userService.getUserByUUID(user.getUserID());
        HashMap<String, Object> map = new HashMap<>();
        map.put("flag",flag);
        map.put("User",userByUUID);
        return JSON.toJSONString(map);
    }
    @RequestMapping("/getAllUserByHealth")
    public String getAllUserByHealth(){
        List<User> allUserByHealth = userService.getAllUserByHealth();
        return JSON.toJSONString(allUserByHealth);
    }
    @RequestMapping("/getInformByUserId")
    public String getInformByUserId(int uid){
        List<Inform> informByUserId = userService.getInformByUserId(uid);
        System.out.println(informByUserId);
        if(informByUserId!=null){
            Collections.reverse(informByUserId); //实现list集合逆序排列
        }
        return JSON.toJSONString(informByUserId);
    }
    @RequestMapping("/addBackHomeApply")
    public String addBackHomeApply(@RequestBody BackHomeApply backHomeApply){
        System.out.println(backHomeApply);
        int i = userService.addBackHomeApply(backHomeApply);
        if (i != 0) {
            return JSON.toJSONString("ok");
        } else {
            return JSON.toJSONString("no");
        }
    }
}
